﻿using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace X.LOG
{
    public static class Log4NetHelper
    {
        static Log4NetHelper()
        {
            var respository = LogManager.CreateRepository(Config.Log4NetRepository);
            XmlConfigurator.ConfigureAndWatch(respository, new System.IO.FileInfo(Config.Log4NetConfigPath));
        }

        private static ILog GetLogger(string name)
        {
            return LogManager.GetLogger(Config.Log4NetRepository, name);
        }
        private static ILog GetLogger(Type type)
        {
            return LogManager.GetLogger(Config.Log4NetRepository, type);
        }


        #region INFO

        /// <summary>
        /// 记录Info日志
        /// </summary>
        /// <param name="message">消息</param>
        public static void Info(string message)
        {
            GetLogger("INFO").Info(message);
        }

        /// <summary>
        /// 记录Info日志
        /// </summary>
        /// <param name="message">消息</param>
        public static void Info(string name, string message)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "INFO" : name).Info(message);
        }

        public static void Info(string name, object obj)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "INFO" : name).Info(obj);
        }

        /// <summary>
        /// 记录Info日志
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="message">消息</param>
        public static void Info(Type type, string message)
        {
            GetLogger(type).Info(message);
        }

        /// <summary>
        /// 记录Info日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Info(Exception ex)
        {
            GetLogger(ex.Source ?? ex.GetType().ToString()).Info(ex.Message, ex);
        }

        public static void Info(string name, Exception ex)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "INFO" : name).Info(ex.Message, ex);
        }

        /// <summary>
        /// 记录Info日志
        /// </summary>
        /// <param name="obj">触发消息的对象类型</param>
        /// <param name="ex">异常</param>
        public static void Info(Type type, Exception ex)
        {
            GetLogger(type).Info(ex.Message, ex);
        }

        #endregion

        #region DEBUG
        /// <summary>
        /// 记录Debug日志
        /// </summary>
        /// <param name="message">消息</param>
        public static void Debug(string message)
        {
            GetLogger("DEBUG").Debug(message);
        }

        /// <summary>
        /// 记录Debug日志
        /// </summary>
        /// <param name="message">消息</param>
        public static void Debug(string name, string message)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "DEBUG" : name).Debug(message);
        }

        public static void Debug(string name, object obj)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "DEBUG" : name).Debug(obj);
        }

        /// <summary>
        /// 记录Debug日志
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="message">消息</param>
        public static void Debug(Type type, string message)
        {
            GetLogger(type).Debug(message);
        }

        /// <summary>
        /// 记录Debug日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Debug(Exception ex)
        {
            GetLogger(ex.Source ?? ex.GetType().ToString()).Debug(ex.Message, ex);
        }

        public static void Debug(string name, Exception ex)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "DEBUG" : name).Debug(ex.Message, ex);
        }

        /// <summary>
        /// 记录Debug日志
        /// </summary>
        /// <param name="obj">触发消息的对象类型</param>
        /// <param name="ex">异常</param>
        public static void Debug(Type type, Exception ex)
        {
            GetLogger(type).Debug(ex.Message, ex);
        }

        #endregion

        #region WARN

        /// <summary>
        /// 记录Warn日志
        /// </summary>
        /// <param name="message">消息</param>
        public static void Warn(string message)
        {
            GetLogger("WARN").Warn(message);
        }

        /// <summary>
        /// 记录Warn日志
        /// </summary>
        /// <param name="message">消息</param>
        public static void Warn(string name, string message)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "WARN" : name).Warn(message);
        }

        public static void Warn(string name, object obj)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "WARN" : name).Warn(obj);
        }

        /// <summary>
        /// 记录Warn日志
        /// </summary>
        /// <param name="obj">触发消息的对象类型</param>
        /// <param name="message">消息</param>
        public static void Warn(Type type, string message)
        {
            GetLogger(type).Warn(message);
        }

        /// <summary>
        /// 记录Warn日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Warn(Exception ex)
        {
            GetLogger(ex.Source ?? ex.GetType().ToString()).Warn(ex.Message, ex);
        }

        public static void Warn(string name, Exception ex)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "WARN" : name).Warn(ex.Message, ex);
        }

        /// <summary>
        /// 记录Warn日志
        /// </summary>
        /// <param name="obj">触发消息的对象类型</param>
        /// <param name="ex">异常</param>
        public static void Warn(Type type, Exception ex)
        {
            GetLogger(type).Warn(ex.Message, ex);
        }
        #endregion

        #region ERROR
        /// <summary>
        /// 记录Error日志
        /// </summary>
        /// <param name="message">消息</param>
        public static void Error(string message)
        {
            GetLogger("ERROR").Error(message);
        }

        /// <summary>
        /// 记录Error日志
        /// </summary>
        /// <param name="message">消息</param>
        public static void Error(string name, string message)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "ERROR" : name).Error(message);
        }

        public static void Error(string name, object obj)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "ERROR" : name).Error(obj);
        }

        /// <summary>
        /// 记录Error日志
        /// </summary>
        /// <param name="obj">触发消息的对象类型</param>
        /// <param name="message">消息</param>
        public static void Error(Type type, string message)
        {
            GetLogger(type).Error(message);
        }

        /// <summary>
        /// 记录Error日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Error(Exception ex)
        {
            GetLogger(ex.Source ?? ex.GetType().ToString()).Error(ex.Message, ex);
        }

        public static void Error(string name, Exception ex)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "ERROR" : name).Error(ex.Message, ex);
        }

        /// <summary>
        /// 记录Error日志
        /// </summary>
        /// <param name="obj">触发消息的对象类型</param>
        /// <param name="ex">异常</param>
        public static void Error(Type type, Exception ex)
        {
            GetLogger(type).Error(ex.Message, ex);
        }

        #endregion

        #region FATAL

        /// <summary>
        /// 记录Fatal日志
        /// </summary>
        /// <param name="message">消息</param>
        public static void Fatal(string message)
        {
            GetLogger("FATAL").Fatal(message);
        }

        /// <summary>
        /// 记录Fatal日志
        /// </summary>
        /// <param name="message">消息</param>
        public static void Fatal(string name, string message)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "FATAL" : name).Fatal(message);
        }

        public static void Fatal(string name, object obj)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "FATAL" : name).Fatal(obj);
        }

        /// <summary>
        /// 记录Fatal日志
        /// </summary>
        /// <param name="obj">触发消息的对象类型</param>
        /// <param name="message">消息</param>
        public static void Fatal(Type type, string message)
        {
            GetLogger(type).Fatal(message);
        }

        /// <summary>
        /// 记录Fatal日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Fatal(Exception ex)
        {
            GetLogger(ex.Source ?? ex.GetType().ToString()).Fatal(ex.Message, ex);
        }

        public static void Fatal(string name, Exception ex)
        {
            GetLogger(string.IsNullOrEmpty(name) ? "FATAL" : name).Fatal(ex.Message, ex);
        }

        /// <summary>
        /// 记录Fatal日志
        /// </summary>
        /// <param name="obj">触发消息的对象类型</param>
        /// <param name="ex">异常</param>
        public static void Fatal(Type type, Exception ex)
        {
            GetLogger(type).Fatal(ex.Message, ex);
        }

        #endregion
    }
}
